﻿using DragonBones;

using UnityEngine;
using UnityEngine.AI;

namespace Assets.XyzCode.Fight.Game.Enemy
{
    partial class Enemy_X_Behav
    {
        /** 面向右侧 */
        private readonly Quaternion FACE_TO_R = new Quaternion(0, 0, 0, 1);

        /** 面向左侧 */
        private readonly Quaternion FACE_TO_L = new Quaternion(0, 180, 0, 1);

        /** 自动寻路 */
        private NavMeshAgent _navMeshAgent;

        /// <summary>
        /// 执行跑动
        /// </summary>
        /// <param name="targetPos">目标位置</param>
        public void DoRun(Vector3 targetPos)
        {
            if (!CanSwitchStateTo(StateEnum.Run))
            {
                return;
            }

            _currState = StateEnum.Run;
            PlayDBAnim("Run", -1, 1.4f);

            if (null == _navMeshAgent)
            {
                _navMeshAgent = gameObject.GetComponent<NavMeshAgent>();
            }

            if (null == _navMeshAgent)
            {
                Debug.LogError("导航网格代理为空");
                return;
            }

            if (targetPos.x > transform.position.x)
            {
                transform.localRotation = FACE_TO_R;
                _VIEW.GetComponent<UnityArmatureComponent>().armature.flipX = false;
            }
            else
            {
                // 修改转向并修改 VIEW 的方向
                transform.localRotation = FACE_TO_L;
                _VIEW.GetComponent<UnityArmatureComponent>().armature.flipX = true;
            }

            _navMeshAgent.enabled = true;
            _navMeshAgent.isStopped = false;
            _navMeshAgent.SetDestination(targetPos);
        }
    }
}
